package com.ruikong.dazhi.controller.base;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.ruikong.dazhi.controller.result.BaseResult;
import com.ruikong.dazhi.controller.result.ResultApi;
import com.ruikong.dazhi.entity.base.Personnel;
import com.ruikong.dazhi.model.input.UserSearchPageInput;
import com.ruikong.dazhi.model.output.PersonnelOutput;
import com.ruikong.dazhi.service.base.IPersonnelService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping(value = "/data")
@EnableAutoConfiguration
@Api(tags = "7.6人员(人员/账号操作)")
public class PersonnelController {

    private static final Logger logger = LoggerFactory.getLogger(PersonnelController.class);

    @Autowired
    private IPersonnelService personnelService;

    /**
     * 添加账号
     * @return
     */
    @ApiOperation(value = "添加账号")
    @RequestMapping(value = "/addPersonAccount",method = RequestMethod.POST)
    public BaseResult addPersonAccount(@RequestBody Personnel personnel) {
        BaseResult result = new BaseResult();
        try {
            int effect = personnelService.addPersonAccount(result, personnel);
            if(effect > 0){
                result.setCode(ResultApi.successCode);
                result.setMsg(ResultApi.addSuccessMsg);
            } else {
                result.setCode(ResultApi.failCode);
            }
        }catch (Exception e){
            logger.error("addPersonAccount", e.getMessage());
            result.setCode(ResultApi.failCode);
            result.setMsg(ResultApi.addFailMsg);
        }
        return result;
    }

    /**
     * 更新账号
     * @return
     */
    @ApiOperation(value = "更新账号")
    @RequestMapping(value = "/updPersonAccount",method = RequestMethod.POST)
    public BaseResult updPersonAccount(@RequestBody Personnel personnel) {
        BaseResult result = new BaseResult();
        try {
            if (personnelService.isExist(personnel)>0){
                result.setCode(ResultApi.failCode);
                result.setMsg("该登录账号已经存在!");
                return result;
            }
            int effect = personnelService.updateAccount(personnel);
            if(effect > 0){
                result.setCode(ResultApi.successCode);
                result.setMsg(ResultApi.addSuccessMsg);
            } else {
                result.setCode(ResultApi.failCode);
            }
        }catch (Exception e){
            logger.error("updPersonAccount", e.getMessage());
            result.setCode(ResultApi.failCode);
            result.setMsg(ResultApi.addFailMsg);
        }
        return result;
    }

    /**
     * 通过session获取账号信息
     * @param httpSession
     * @return
     */
    @ApiOperation(value = "获取当前账号信息")
    @RequestMapping(value = "/getPersonnel",method = RequestMethod.POST)
    public BaseResult getPersonnel(HttpSession httpSession, HttpServletRequest request, HttpServletResponse response) {
        BaseResult result = new BaseResult();

        try {
            Object object = httpSession.getAttribute("outPersonnel");
            if(object==null){
                response.sendRedirect(request.getContextPath() + "/view/login");
            }
            PersonnelOutput personnel = (PersonnelOutput) object;
            result.setCode(ResultApi.successCode);
            result.setMsg(ResultApi.getSuccessMsg);
            result.setData(personnel);
        }catch (Exception e){
            result.setCode(ResultApi.failCode);
            result.setMsg(ResultApi.getFailMsg);
            logger.error("getPersonnel", e.getMessage());
        }
        return result;
    }

    /**
     * 获取账号
     *
     * @param input
     * @return
     */
    @ApiOperation(value = "获取列表_分页")
    @RequestMapping(value = "/getPersonnelListPage", method = RequestMethod.POST)
    public PageInfo<PersonnelOutput> getPersonnelListPage(@RequestBody UserSearchPageInput input) {
        PageInfo<PersonnelOutput> pageInfo = new PageInfo<>();
        try {
            pageInfo = personnelService.getPersonnelListPage(input);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return pageInfo;
    }

    /**
     * 获取人员列表
     * @return
     */
    @ApiOperation(value = "获取人员列表")
    @RequestMapping(value = "/getPersonnelList", method = RequestMethod.POST)
    public BaseResult getPersonnelList() {
        BaseResult result = new BaseResult();

        try {
            List<Personnel> personals = personnelService.getPersonnelList();
            result.setCode(ResultApi.successCode);
            result.setMsg(ResultApi.getSuccessMsg);
            result.setData(personals);
            //所有分页属性都可以从pageInfoDemo拿到；
        } catch (Exception e) {
            logger.error("getPersonnelList", e.getMessage());
            result.setCode(ResultApi.failCode);
            result.setMsg(ResultApi.getFailMsg);
        }

        return result;
    }

    /**
     * 删除账号
     *
     * @param personnel
     * @return
     */
    @ApiOperation(value = "删除账号")
    @RequestMapping(value = "/deleteAccount", method = RequestMethod.POST)
    public BaseResult deleteAccount(@RequestBody Personnel personnel) {

        BaseResult result = new BaseResult();
        try {
            int effectLine = personnelService.deleteAccount(personnel);
            if (effectLine > 0) {
                result.setCode(ResultApi.successCode);
                result.setMsg(ResultApi.deleteSuccessMsg);
            } else {
                result.setCode(ResultApi.failCode);
                result.setMsg(ResultApi.deleteFailMsg);
            }

        } catch (Exception e) {
            logger.error("deleteAccount", e.getMessage());
            result.setCode(ResultApi.failCode);
            result.setMsg(ResultApi.deleteFailMsg);
        }

        return result;
    }

    /**
     * 修改密码
     *
     * @param jsonObject
     * @return
     */
    @ApiOperation(value = "更新密码")
    @RequestMapping(value = "/updatePassword", method = RequestMethod.POST)
    public BaseResult updatePassword (@RequestBody JSONObject jsonObject, HttpSession httpSession) {
        BaseResult result = new BaseResult();
        try {
            int effect = personnelService.updatePassword(jsonObject,result,httpSession);
            if(effect > 0){
                result.setCode(ResultApi.successCode);
            } else {
                result.setCode(ResultApi.failCode);
            }
        } catch (Exception e) {
            logger.error("updatePassword", e.getMessage());
            result.setCode(ResultApi.failCode);
            result.setMsg(ResultApi.updateFailMsg);
        }
        return result;
    }

    @ApiOperation(value = "重置密码")
    @RequestMapping(value = "/resetPassword", method = RequestMethod.POST)
    public BaseResult resetPassword (@RequestBody   Personnel personnel, HttpSession httpSession) {
        BaseResult result = new BaseResult();
        try {
            int effect = personnelService.resetPassword(personnel,result,httpSession);
            if(effect > 0){
                result.setCode(ResultApi.successCode);
            } else {
                result.setCode(ResultApi.failCode);
            }
        } catch (Exception e) {
            logger.error("updatePassword", e.getMessage());
            result.setCode(ResultApi.failCode);
            result.setMsg(ResultApi.updateFailMsg);
        }
        return result;
    }

    /**
     * 启用帐号
     */
    @ApiOperation(value = "启用帐号")
    @RequestMapping(value = "/invokePersonnel", method = RequestMethod.POST)
    public BaseResult invokePersonnel(@RequestBody Personnel personnel) {
        BaseResult result = new BaseResult();
        try {
            int effect = personnelService.invokePersonnel(personnel);
            if(effect > 0){
                result.setCode(ResultApi.successCode);
                result.setMsg("启用成功");
            } else {
                result.setCode(ResultApi.failCode);
                result.setMsg("启用失败");
            }
        } catch (Exception e) {
            logger.error("invokePersonnel", e.getMessage());
            result.setCode(ResultApi.failCode);
            result.setMsg("启用失败");
        }
        return result;
    }

    /**
     * 停用帐号
     */
    @ApiOperation(value = "停用帐号")
    @RequestMapping(value = "/inactivePersonnel", method = RequestMethod.POST)
    public BaseResult inactivePersonnel(@RequestBody Personnel personnel) {
        BaseResult result = new BaseResult();
        try {
            int effect = personnelService.inactivePersonnel(personnel);
            if(effect > 0){
                result.setCode(ResultApi.successCode);
                result.setMsg("停用成功");
            } else {
                result.setCode(ResultApi.failCode);
                result.setMsg("停用失败");
            }
        } catch (Exception e) {
            logger.error("inactivePersonnel", e.getMessage());
            result.setCode(ResultApi.failCode);
            result.setMsg("停用失败");
        }
        return result;
    }

    /**
     * 根据账号ID获取账号
     */
    @ApiOperation(value = "根据账号ID获取账号")
    @RequestMapping(value = "/getPersonnelById", method = RequestMethod.POST)
    public BaseResult getPersonnelById(@RequestBody Personnel personnel) {
        BaseResult result = new BaseResult();
        try {
            Personnel p = personnelService.getByPersonnelId(personnel.getPersonnelId());
            if(p!=null){
                result.setCode(ResultApi.successCode);
                result.setMsg(ResultApi.getSuccessMsg);
                result.setData(p);
            } else {
                result.setCode(ResultApi.failCode);
                result.setMsg(ResultApi.getFailMsg);
            }
        } catch (Exception e) {
            logger.error("getPersonnelById", e.getMessage());
            result.setCode(ResultApi.failCode);
            result.setMsg("调用账号信息失败");
        }
        return result;
    }

}
